package com.cqdev.washer.api.rest.controller;

import com.cqdev.washer.api.rest.model.ResponseBean;
import com.cqdev.washer.api.rest.model.StatusCode;
import com.cqdev.washer.biz.model.ProjectBO;
import com.cqdev.washer.biz.service.ExecuteService;
import com.cqdev.washer.biz.service.ProjectService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.cqdev.washer.biz.enums.ProjectExecuteStatusEnum.DOING;

/**
 * @author yunfei.li
 *         类描述: TODO
 *         日期： 2018/7/16
 */
@RestController("restProjectController")
@RequestMapping("/rest/project")
public class ProjectController {

    private final ProjectService projectService;
    private final ExecuteService executeService;

    public ProjectController(ProjectService projectService, ExecuteService executeService) {
        this.projectService = projectService;
        this.executeService = executeService;
    }

    @GetMapping("/status/{id}")
    public ResponseEntity getStatus(@PathVariable("id") Long projectId) {

        ProjectBO projectBO = projectService.getOne(projectId);

        if (projectBO == null) {
            return new ResponseEntity(new ResponseBean<>(StatusCode.DATA_NOT_EXIST), HttpStatus.NOT_FOUND);
        }


        return ResponseEntity.status(projectBO.getProjectExecuteStatusEnum().code()).body(new ResponseBean(projectBO.getProjectExecuteStatusEnum()));
    }

    @GetMapping("/start/{id}")
    public ResponseEntity start(@PathVariable("id") Long projectId) {

        ProjectBO one = projectService.getOne(projectId);
        if (one == null) {
            return new ResponseEntity(new ResponseBean<>(StatusCode.DATA_NOT_EXIST), HttpStatus.NOT_FOUND);
        }
        if (one.getProjectExecuteStatusEnum() == DOING) {
            return ResponseEntity.status(DOING.code()).body(new ResponseBean<>(DOING.code(), DOING.message()));
        }
        executeService.exec(projectId);

        return new ResponseEntity(new ResponseBean<>("SUCCESS"), HttpStatus.OK);
    }

}


